package com.hymall.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketCollect;
import com.hymall.bean.MarketCollectExample;
import com.hymall.bean.MarketGoods;
import com.hymall.bean.MarketUser;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.WxCollectVo;
import com.hymall.mapper.MarketCollectMapper;
import com.hymall.mapper.MarketGoodsMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 周子伟
 * @description ${}
 * @date: 2022/7/19 15:18
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;


    /*
     * @author Invoker
     * @description {添加或解除收藏}
     * @date 2022/7/19 16:57
     * @param [marketCollect]
     * @return com.hymall.bean.MarketCollect
     */
    @Override
    public MarketCollect queryCollectlistByValueId(@RequestBody MarketCollect marketCollect) {

        marketCollectMapper.selectByValueId(marketCollect.getValueId());


        return marketCollect;
    }

    @Override
    public boolean deleteCollect(MarketCollect marketCollect) {
        marketCollectMapper.deleteByValueId(marketCollect.getValueId());

        return true;
    }

    @Override
    public boolean addCollect(MarketCollect marketCollect) {
        marketCollectMapper.insertSelective(marketCollect);

        return true;
    }

    /*
     * @author Invoker
     * @description {显示收藏}
     * @date 2022/7/19 21:50
     * @param [type, info]
     * @return com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData listAllCollect(Byte type, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketCollectExample marketCollectExample = new MarketCollectExample();
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        marketCollectExample.createCriteria().andUserIdEqualTo(userId);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        List<WxCollectVo> wxCollectVos = new ArrayList<>();
        for (MarketCollect marketCollect : marketCollects) {
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(marketCollect.getValueId());
            WxCollectVo wxCollectVo = new WxCollectVo(marketGoods.getBrief(), marketGoods.getPicUrl(), marketCollect.getValueId(), marketGoods.getName(),
                    marketCollect.getId(), marketCollect.getType(), marketGoods.getRetailPrice());
            wxCollectVos.add(wxCollectVo);
        }

        PageInfo<WxCollectVo> pageInfo = new PageInfo<>(wxCollectVos);

        CommonData data = CommonData.data(pageInfo);

        return data;
    }

    @Override
    public boolean addordelete(MarketCollect marketCollect) {

        if (marketCollectMapper.selectByValueId(marketCollect.getValueId()) == null) {
            Date date = new Date();
            marketCollect.setAddTime(date);
            marketCollect.setUpdateTime(date);

            Subject subject = SecurityUtils.getSubject();
            MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = user.getId();
            marketCollect.setUserId(userId);
            marketCollectMapper.insertSelective(marketCollect);

        } else {
            Date date = new Date();
            marketCollect.setUpdateTime(date);
            marketCollectMapper.deleteByValueId(marketCollect.getValueId());

        }


        return false;
    }


}
